﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data.SqlClient;
using System.Text;
using prototype123.models;
using prototype123.controllers;
using System.Collections;

namespace prototype123.data_access
{
    //This class handles all data access between the application and the
    //student table in the database.
    class StudentDAO
    {
        private SqlConnection db_connection;// = prototype123.Program.connect.GetConnection();

        public StudentDAO(SqlConnection connection)
        {
            db_connection = connection;
        }


        /*precondition: system has an educator ID and needs all students associated with that educator
        description: given an educator ID (int id), Load retrieves all students associated with that educator.
              the reader will get 1 student at a time, so loop through and add each student
              object into an arraylist, and return the arraylist of students.
        postcondition: the system receives an ArrayList of student objects - one object for each student
              that the educator is responsible for*/
        public ArrayList Load(int id)
        {
            SqlCommand getEd;
            ArrayList list = new ArrayList();

            try
            {
                getEd = new SqlCommand("SELECT * FROM STUDENT WHERE EDU_ID = " + id, db_connection);
                SqlDataReader reader = getEd.ExecuteReader();
                while (reader.Read())
                {
                    Student tmp = new Student();
                    tmp.StudentID = Convert.ToInt32(reader["STU_ID"]);
                    tmp.FirstName = (string)reader["FNAME"];
                    tmp.LastName = (string)reader["LNAME"];
                    tmp.MiddleInitial = Convert.ToChar(reader["MI"]);
                    tmp.GoesBy = (string)reader["GOESBY"];
                    tmp.StatusCD = Convert.ToChar(reader["STATUSCD"]);
                    tmp.Grade = Convert.ToInt32(reader["GRADE"]);
                    tmp.TrackID = Convert.ToInt32(reader["TRACK_ID"]);
                    list.Add(tmp);
                }
                reader.Close();
            }
            catch { list = null; }
            return list;
        }

    }
}
